require "arknights/utils"

local _data ,infra, mission, staging, visit
local staging_prog

local app_name = "com.hypergryph.arknights.bilibili" -- B服应用id
local screen_prot_name = "tecelecta.screenprotect"
local exception_dialog = {x=965, y=760}
local daily_hint = { -- 每日奖励窗口
    {x=1805, y=115, c=0xbfbfbf},
    {x=1805, y=910, c=0xf4b800},
    {x=170, y=135, c=0xffffff}
}
local ack_msg_hint = { -- 通知窗口
    {x=1854, y=84, c=0xbfbfbf},
    {x=54, y=84, c=0x111111},
    {x=54, y=217, c=0xa0a0a}
}
local home_hint = {
    {x=74, y=70, c=0xffffff},
    {x=180, y=70, c=0xffffff},
    {x=293, y=70, c=0xffffff},
    {x=402, y=70, c=0xffffff}
}
local shortcut_button = {x=400, y=55, c=0xD8D8D8} -- home按钮
-- 主界面按钮
local home_staging = {x=1250, y=270}
local home_infra = {x=1540, y=920}
local home_mission = {x=400, y=55, c=0xD8D8D8}
-- 快捷按钮， 这些组件是居中对齐的，因此在增宽屏幕中，x + 1/2dx
local staging_shortcut = {x=795, y=163, c=0x606060}
local home_shortcut = {x=140, y=255, c=0xd6d6d1}
local mission_shortcut = {x=1630, y=60, c=0xffffff}
local friend_shortcut = {x=1810, y=65, c=0xffffff}

local infra_hint = {x=1096, y=45, c=0xdc92ff}

local spfile = userPath() .. "/res/sprog.json"
local cross = userPath() .. "/res/optags/cross.png"

local function shortcut_jump(shortcut)
    waitForColor(shortcut_button, 500)
    waitForAllColors({friend_shortcut, staging_shortcut, mission_shortcut}, 500,
        function () 
            qcRandTap(shortcut_button, 20, 500)
        end    
    )
    waitWhileColor(shortcut, 350,
        function ()
            qcRandTap(shortcut, 5, 1200)
        end    
    )
    if shortcut ~= home_shortcut then
        waitForColor(shortcut_button, 500)
    end
end


local function stage_on_plan()
    local san_consumed = false
    for stage, plan_count in pairs(_data.staging_plan) do
        local do_cnt = plan_count
        if type(staging_prog[stage]) == "number" then
            do_cnt = do_cnt - staging_prog[stage]
        else
            staging_prog[stage] = 0
        end

        shortcut_jump(staging_shortcut)
        log(string.format("[DATE] autostaging by main.lua =========================================="), "staging")
        local done_cnt = 0
        if do_cnt > 0 then 
            done_cnt = staging.run(stage, do_cnt)
        end
        if done_cnt == -1 then
            toast(string.format("今日%s未开放", stage))
        elseif type(staging_prog[stage]) == "number" then
            staging_prog[stage] = staging_prog[stage] + done_cnt
        else
            dialog("[DEBUG] this is literally not gonna happen", 0) 
            staging_prog[stage] = done_cnt
        end
        if staging_prog[stage] < plan_count and done_cnt ~= -1 then
            -- 没能完成计划任务，说明理智已经耗尽
            _data.dump_json(staging_prog, spfile)
            break
        end
        -- 减少突发情况带来的损失
        _data.dump_json(staging_prog, spfile)
    end
end

local function daily_1_11()
    shortcut_jump(staging_shortcut)
    staging.run('1-11', 2)
end

local function initialize()
    _data = require("arknights/data")
    infra = require("arknights/arknights_auto_infra")
    mission = require("arknights/arknights_auto_mission")
    staging = require("arknights/arknights_auto_staging")
    visit = require("arknights/arknights_auto_visit")
    staging_prog = _data.load_json(spfile)
end

local arknights_module = {}

arknights_module.main = function()
    init(1)
    -- the entrance of the game logic, init needed
    while isFrontApp(app_name) == 0 do
        closeApp(app_name)
        closeApp(screen_prot_name)
        mSleep(3000)
        runApp(app_name, true)
        mSleep(10000)
        waitForAllColors(home_hint, 1000, 
            function ()
                qcRandTap(exception_dialog)
            end
        )
    end
    -- home -> infra
    waitForColor(infra_hint, 500, 
        function ()
            if qcAllIsColor(ack_msg_hint) then
                qcRandTap(ack_msg_hint[1], 10, 1000)
            elseif qcAllIsColor(daily_hint) then
                qcRandTap(daily_hint[1], 10, 1000)
            elseif foundPattern({x1=1755, x2=1900, y1=45, y2=200}, cross, 0.05) then
                local _, x, y = foundPattern({x1=1755, x2=1900, y1=45, y2=200}, cross, 0.05)
                qcRandTap({x=1755+x+5, y=45+y+5}, 5, 1000)
            else
                qcRandTap(home_infra, 20, 1200)
            end
        end    
    )
    infra.run()

    -- infra -> visit
    shortcut_jump(friend_shortcut)
    visit.run()

    -- visit -> staging
    stage_on_plan()
    daily_1_11()
    -- visit -> mission
    shortcut_jump(mission_shortcut)
    mission.run()

    shortcut_jump(home_shortcut)
    while isFrontApp(screen_prot_name) == 0 do
        runApp(screen_prot_name, true)
        mSleep(1000)
    end
end

if __name__ ~= "main" then
    __name__ = "main"
    initialize()

    UINew({width=800,height=600})
    UILabel("PSTD-Extention module")
    UIRadio("sel", "延迟模式,执行模式")
    UIShow()

    if sel == "延迟模式" then
        while isFrontApp(screen_prot_name) == 0 do
            runApp(screen_prot_name, true)
            mSleep(1000)
        end
        doAt({"08:00","16:00","00:00"}, arknights_module.main)
    else
        arknights_module.main()
    end
else
    initialize()
    return arknights_module
end

